package com.leyou.item.web;

import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("spec")
public class SpecController {

    @Autowired
    private SpecGroupService groupService;
    @Autowired
    private SpecParamService paramService;


    /**
     * todo 根据商品 分类id 查询规格组的集合
     * @param id
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>>querySpecgroupByCid(@RequestParam("id")Long id){

        return ResponseEntity.ok(groupService.querySpecgroupByCid(id));
    }


    /**
     * todo 根据 条件 (规格参数组的id) 查询规格参数集合
     * @param gid
     * @param cid
     * @param searching
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>>queryParams(
            @RequestParam(value = "groupId",required = false)Long gid,
            @RequestParam(value="categoryId",required = false)Long cid,
            @RequestParam(value = "searching",required = false)Boolean searching){
        return ResponseEntity.ok(paramService.queryParams(gid,cid,searching));
    }


     /**
     * todo 根据 分类id 查询对应的  规格参数组 以及 其组内参数 ,以及 每个参数 对应得具体的值
     * @param id
     * @return   12.9上午写
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> listSpecGoupByCId(@RequestParam("id") Long cid){

        return ResponseEntity.ok(groupService.listSpecGoupByCId(cid));

    }




    /**
     * todo 新增 规格组
     * @param groupDTO 规格组的id不是在自增的嘛 为什么也要传进来
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> addGroup(@RequestBody SpecGroupDTO groupDTO){
        groupService.addGroup(groupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * todo 修改规格组
     * @param groupDTO
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void>updateGroup(@RequestBody SpecGroupDTO groupDTO){

        groupService.updateGroup(groupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * todo 新增规格参数
     * @param paramDTO
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity<Void>addParam(@RequestBody SpecParamDTO paramDTO){

        paramService.addParam(paramDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * todo 修改 规格参数
     * @param paramDTO
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void>updateParam(@RequestBody SpecParamDTO paramDTO){

        paramService.updateParam(paramDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }




}
